import { queryOwner, queryAlarmToday, queryMonitor, queryIncidence, queryPosition, queryFire, queryDanger, queryFace } from './service';

export default {
  namespace: 'home',

  state: {
    alarmToday: {},
    monitor: [],
    incidence: [],
    position: [],
    fire: {},
    face: {},
    owner: {}
  },

  effects: {
    *queryOwner(_, { call, put }) {
      const response = yield call(queryOwner);
      yield put({ type: 'setOwner', payload: response })
    },
    *queryAlarmToday(_, { call, put }) {
      const [a1, a2] = yield call(queryAlarmToday);
      yield put({ type: 'setAlarmToday', payload: { ...a1, ...a2 } })
    },
    *queryMonitor(_, { call, put }) {
      const response = yield call(queryMonitor);
      yield put({ type: 'setMonitor', payload: Array.isArray(response) ? response : [] })
    },
    *queryIncidence(_, { call, put }) {
      const response = yield call(queryIncidence);
      yield put({ type: 'setIncidence', payload: Array.isArray(response) ? response : [] })
    },
    *queryPosition(_, { call, put }) {
      const response = yield call(queryPosition);
      yield put({ type: 'setPosition', payload: Array.isArray(response) ? response : [] })
    },
    *queryFire(_, { call, put }) {
      const response = yield call(queryFire);
      yield put({ type: 'setFire', payload: response });
    },
    *queryDanger(_, { call, put }) {
      const response = yield call(queryDanger);
      yield put({ type: 'setDanger', payload: response });
    },
    *queryFace({ payload }, { call, put, select }) {

      const response = yield call(queryFace, payload);
      yield put({ type: 'setFace', payload: response })
    }
  },

  reducers: {
    setOwner(state, action) {
      return {
        ...state,
        owner: action.payload
      }
    },
    
    setAlarmToday(state, action) {
      return {
        ...state,
        alarmToday: action.payload
      }
    },

    setMonitor(state, action) {
      return {
        ...state,
        monitor: action.payload
      }
    },

    setIncidence(state, action) {
      return {
        ...state,
        incidence: action.payload
      }
    },

    setPosition(state, action) {
      return {
        ...state,
        position: action.payload
      }
    },

    setFire(state, action) {
      return {
        ...state,
        fire: action.payload
      }
    },

    setDanger(state, action) {
      return {
        ...state,
        danger: action.payload
      }
    },

    setFace(state, action) {
      return {
        ...state,
        face: action.payload
      }
    }
  },
};
